using System.Linq.Expressions;
using FastDevTemplate.Api.Base;
using FastDevTemplate.Api.Model;
using FastDevTemplate.Api.Model.Base;
using FastDevTemplate.DataAccess.Entities;
using FastDevTemplate.Service.Interface;
using FastDevTemplate.Service.Interface.Base;
using FastDevTemplate.Service.Interface.DTO;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FastDevTemplate.Api.Controllers;


[EnableCors("AllowSpecificOrigin")]
public class LogController : BaseController
{
    private readonly ILogService _logService;
    
    public LogController(ILogService logService)
    {
        _logService = logService;
    }
    
    [HttpPost("getListByQuery")]
    public async Task<HttpResult<PageResultDto<LogResultDto>>> GetListByQuery([FromBody]PageRequestModel<LogQueryModels> req)
    {
        var expressionList = DeserializeQueryParameters<Log>(
            req.QueryItem, 
            ExpressionType.Contains, 
            [nameof(req.QueryItem.StartTime), nameof(req.QueryItem.EndTime)]);
        if (req.QueryItem != null)
        {
            expressionList.Add(item => item.DateTime >= req.QueryItem.StartTime);
            expressionList.Add(item => item.DateTime < req.QueryItem.EndTime);
        }
        var result = await _logService.GetPageAsync(req.PageIndex, req.PageSize, expressionList.ToArray());
        return OkResult(result);
    }    
}